package at.ac.tuwien.swa.sparrow.server.security

import at.ac.tuwien.swa.sparrow.server.security.SecurityContext.Role
import java.io.Serializable

object SecurityContext extends SecurityContext {

	lazy val global = new GlobalSecurityContext
	lazy val threadLocal = new ThreadLocalSecurityContext
	var strategy: SecurityContext = threadLocal

	type Role = AnyRef with Serializable

	override def setRole(role: Role) = strategy.setRole(role)

	override def getRole = strategy.getRole
}

sealed trait SecurityContext {

	def setRole(role: Role)

	def getRole: Role
}

private[security] class ThreadLocalSecurityContext extends SecurityContext {
	private val THREAD_ROLE = new ThreadLocal[Role]()

	def setRole(role: Role) = THREAD_ROLE.set(role)

	def getRole = THREAD_ROLE.get
}

private[security] class GlobalSecurityContext extends SecurityContext {
	var role: Role = null

	def setRole(role: Role) = this.role = role

	def getRole = role
}
